package com.yebxxx.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yebxxx.server.mapper.EmployeeMapper;
import com.yebxxx.server.pojo.*;
import com.yebxxx.server.service.IEmployeeRemoveService;
import com.yebxxx.server.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author stx
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/employee-remove")
public class EmployeeRemoveController {
    @Autowired
    private IEmployeeRemoveService employeeRemoveService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @GetMapping("/")
    @ApiOperation(value = "员工调动列表")
    public PageBean RemoveList(@RequestParam(defaultValue = "1" )Integer currentPage,
                               @RequestParam(defaultValue = "10")Integer size,
                               EmployeeRemove employeeRemove){
        return employeeRemoveService.RemoveList(currentPage,size,employeeRemove);
    }

    @PostMapping
    @ApiOperation(value = "添加员工调动")
    public  RespBean addEmpRemove(EmployeeRemove employeeRemove,Integer id){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",id);
        Employee employee = employeeMapper.selectOne(queryWrapper);
//        Employee employeeServiceById = employeeService.getById(queryWrapper);
        employeeRemove.setWorkId(employee.getWorkID());
        employeeRemove.setName(employee.getName());
        employeeRemove.setRemoveDate(LocalDate.now());
        employee.setDepartmentId(employeeRemove.getAfterDepId());
        employeeService.save(employee);
//        System.out.println(employee+"==============");
        if( employeeRemoveService.save(employeeRemove)){
            return RespBean.success("调动成功");

        }
        return  RespBean.error("调动失败");
    }

    @ApiOperation(value = "删除员工调动")
    @DeleteMapping("/")
    public RespBean deleteRemoveList(Integer id){
//        QueryWrapper queryWrapper=new QueryWrapper();
//        queryWrapper.eq("workId",workId);

        if( employeeRemoveService.removeById(id)){
            return  RespBean.success("删除成功！");
        }
        return  RespBean.error("删除失败!");
    }
    @ApiOperation(value = "修改员工调动")
    @PostMapping("/updateEmpRemove")
    public RespBean updateEmpRemove(@RequestBody EmployeeRemove employeeRemove){
        Employee employee=new Employee();
        employeeRemove.setRemoveDate(LocalDate.now());
        if(employeeRemoveService.updateById(employeeRemove)){
            Integer depId = employeeRemove.getAfterDepId();
            String workId = employeeRemove.getWorkId();
            employeeService.updateEmpDep(workId,depId);
            System.out.println("======>"+employeeRemove);
            System.out.println("======>"+employee);
            return RespBean.success("更新成功！");
        }
        return  RespBean.error("更新失败！");

    }

}
